﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace GSManager
{
    public class DangNhap
    {
        private string _TaiKhoan;
        private string _MatKhau;
        private string _TenTaiKhoan;
        private int _CapDo;
        public string TaiKhoan
        {
            get { return _TaiKhoan; }
            set { _TaiKhoan = value; }
        }
        public string MatKhau
        {
            get { return _MatKhau; }
            set { _MatKhau = value; }
        }
        public string TenTaiKhoan
        {
            get { return _TenTaiKhoan; }
            set { _TenTaiKhoan = value; }
        }
        public int CapDo
        {
            get{ return _CapDo;}
            set{ _CapDo=value;}
        }
        public bool KiemTraTaiKhoan()
        {
            OleDbConnection ketnoi = KetNoi.KetNoiDuLieu();
            string sql = "select TaiKhoan from TAIKHOAN where TaiKhoan=?";
            OleDbCommand cmd = new OleDbCommand(sql, ketnoi);
            cmd.Parameters.Add("@TaiKhoan", OleDbType.VarWChar);
            cmd.Parameters[0].Value = TaiKhoan;
            OleDbDataReader r = cmd.ExecuteReader();
            bool kq = r.Read();
            r.Close();
            ketnoi.Close();
            return kq;
        }
        public bool KiemTraMatKhau()
        {
            OleDbConnection ketnoi = KetNoi.KetNoiDuLieu();
            string sql = "select TaiKhoan from TAIKHOAN where MatKhau=? and TaiKhoan=?";
            OleDbCommand cmd = new OleDbCommand(sql, ketnoi);
            cmd.Parameters.Add("@MatKhau", OleDbType.VarChar);
            cmd.Parameters.Add("@TaiKhoan", OleDbType.VarChar);
            cmd.Parameters[0].Value = MatKhau;
            cmd.Parameters[1].Value = TaiKhoan;
            OleDbDataReader r = cmd.ExecuteReader();
            bool kq = r.Read();
            r.Close();
            ketnoi.Close();
            return kq;
        }
        public DangNhap LayThongTinTaiKhoan()
        {
            OleDbConnection ketnoi = KetNoi.KetNoiDuLieu();
            string sql = "select TenTaiKhoan,CapDo,TaiKhoan from TAIKHOAN where TaiKhoan=?";
            OleDbCommand cmd=new OleDbCommand(sql,ketnoi);
            cmd.Parameters.Add("@TaiKhoan",OleDbType.VarChar);
            cmd.Parameters[0].Value = TaiKhoan;
            OleDbDataReader r=cmd.ExecuteReader();
            DangNhap dangnhap=new DangNhap();
            while(r.Read())
            {
                dangnhap.TenTaiKhoan=(string)r[0];
                dangnhap.CapDo=(int)r[1];
                dangnhap.TaiKhoan = (string)r[2];
            }
            r.Close();
            ketnoi.Close();
            return dangnhap;
        }
        public void ThayDoiMatKhau()
        {
            OleDbConnection ketnoi = KetNoi.KetNoiDuLieu();
            string sql = "Update TAIKHOAN set MatKhau='" + MatKhau + "' where TaiKhoan='"+TaiKhoan+"'";
            OleDbCommand cmd = new OleDbCommand(sql, ketnoi);
            cmd.ExecuteNonQuery();
            ketnoi.Close();
        }
        public void ThemTaiKhoan()
        {
            OleDbConnection ketnoi = KetNoi.KetNoiDuLieu();
            string sql = "Insert into TAIKHOAN values('"+TaiKhoan+"','"+MatKhau+"',?,"+CapDo+")";
            OleDbCommand cmd = new OleDbCommand(sql, ketnoi);
            cmd.Parameters.Add("@TenTaiKhoan", OleDbType.VarWChar);
            cmd.Parameters[0].Value = TenTaiKhoan;
            cmd.ExecuteNonQuery();
            ketnoi.Close();
        }
    }
}
